package com.dwx.guli.service.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dwx.guli.service.base.dto.CourseDto;
import com.dwx.guli.service.base.result.R;
import com.dwx.guli.service.edu.entity.*;
import com.dwx.guli.service.edu.entity.query.CourseQuery;
import com.dwx.guli.service.edu.entity.query.WebCourseQuery;
import com.dwx.guli.service.edu.entity.query.form.CourseInfoForm;
import com.dwx.guli.service.edu.entity.vo.CoursePublishVo;
import com.dwx.guli.service.edu.entity.vo.CourseVo;
import com.dwx.guli.service.edu.entity.vo.WebCourseVo;
import com.dwx.guli.service.edu.feign.OssFileService;
import com.dwx.guli.service.edu.mapper.*;
import com.dwx.guli.service.edu.service.CourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author dwx
 * @since 2021-06-30
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {
    @Autowired
    CourseDescriptionMapper courseDescriptionMapper;
    @Autowired
    OssFileService ossFileService;
    @Autowired
    private VideoMapper videoMapper;
    @Autowired
    private ChapterMapper chapterMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Override
    public IPage<CourseVo> selectPage(Long page, Long limit, CourseQuery courseQuery) {
        QueryWrapper<CourseVo> queryWrapper=new QueryWrapper<>();
        String title=courseQuery.getTitle();
        String teacherId=courseQuery.getTeacherId();
        String subjectParentId=courseQuery.getSubjectParentId();
        String subjectId=courseQuery.getSubjectId();

        queryWrapper.orderByDesc("c.publish_time");
        queryWrapper.like(!StringUtils.isEmpty(title),"c.title",title);
        queryWrapper.eq(!StringUtils.isEmpty(teacherId),"c.teacher_id",teacherId);
        queryWrapper.eq(!StringUtils.isEmpty(subjectParentId),"c.subject_parent_id",subjectParentId);
        queryWrapper.eq(!StringUtils.isEmpty(subjectId),"c.subject_id",subjectId);
        Page<CourseVo> pageParam=new Page<>(page,limit);
        List<CourseVo> courseVos = baseMapper.selectPageByCourseQuery(pageParam, queryWrapper);
        return pageParam.setRecords(courseVos);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public String saveCourseInfo(CourseInfoForm courseInfoForm) {
        Course course=new Course();
        BeanUtils.copyProperties(courseInfoForm,course);
        baseMapper.insert(course);

        String courseId = course.getId();
        CourseDescription courseDescription=new CourseDescription();
        courseDescription.setDescription(courseInfoForm.getDescription());
        courseDescription.setId(courseId);
        courseDescriptionMapper.insert(courseDescription);
        return courseId;
    }

    @Override
    public CourseInfoForm getCourseInfoById(String id) {
        Course course = baseMapper.selectById(id);
        if(course==null){
            return null;
        }
        CourseInfoForm courseInfoForm=new CourseInfoForm();
        BeanUtils.copyProperties(course,courseInfoForm);
        CourseDescription courseDescription = courseDescriptionMapper.selectById(id);
        courseInfoForm.setDescription(courseDescription.getDescription());
        return courseInfoForm;
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateCourseInfoById(CourseInfoForm courseInfoForm) {
        Course course=new Course();
        BeanUtils.copyProperties(courseInfoForm,course);
        baseMapper.updateById(course);

        String courseId = course.getId();
        CourseDescription courseDescription=new CourseDescription();
        courseDescription.setDescription(courseInfoForm.getDescription());
        courseDescription.setId(courseId);
        courseDescriptionMapper.updateById(courseDescription);
    }

    @Override
    public boolean removeCoverById(String id) {
        Course course = baseMapper.selectById(id);
        if(course!=null){
            String cover = course.getCover();
            if(cover!=null){
                R r = ossFileService.removeFile(cover);
                return r.getSuccess();
            }
        }
        return false;
    }

    @Override
    public boolean removeCourseById(String id) {
        QueryWrapper<Video> videoQueryWrapper=new QueryWrapper<>();
        videoQueryWrapper.eq("course_id",id);
        //删除视频
        videoMapper.delete(videoQueryWrapper);

        courseDescriptionMapper.deleteById(id);
        QueryWrapper<Chapter> chapterQueryWrapper=new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id",id);
        chapterMapper.delete(chapterQueryWrapper);
        QueryWrapper<Comment> commentQueryWrapper=new QueryWrapper<>();
        commentQueryWrapper.eq("course_id",id);
        commentMapper.delete(commentQueryWrapper);
        int i = baseMapper.deleteById(id);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public CoursePublishVo getCoursePublishVoById(String id) {
        return baseMapper.selectCoursePublishVoById(id);
    }

    @Override
    public boolean publishCourseById(String id) {
        Course course=new Course();
        course.setId(id);
        course.setPublishTime(new Date());
        course.setStatus(Course.COURSE_NORMAL);
        int i = baseMapper.updateById(course);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public List<Course> webSelectList(WebCourseQuery webCourseQuery) {
        QueryWrapper<Course> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("status",Course.COURSE_NORMAL);
        String subjectParentId = webCourseQuery.getSubjectParentId();
        String subjectId = webCourseQuery.getSubjectId();
        String publishTimeSort = webCourseQuery.getPublishTimeSort();
        String priceSort = webCourseQuery.getPriceSort();
        String buyCountSort = webCourseQuery.getBuyCountSort();
        //`subject_id``subject_parent_id``buy_count``publish_time``price`
        queryWrapper.eq(!StringUtils.isEmpty(subjectId),"subject_id",subjectId);
        queryWrapper.eq(!StringUtils.isEmpty(subjectParentId),"subject_parent_id",subjectParentId);
        queryWrapper.orderByDesc(!StringUtils.isEmpty(publishTimeSort),"publish_time");
        queryWrapper.orderByDesc(!StringUtils.isEmpty(buyCountSort),"buy_count");
        if(!StringUtils.isEmpty(priceSort)){
            if("1".equals(priceSort)){
                queryWrapper.orderByAsc("price");
            }else {
                queryWrapper.orderByDesc("price");
            }
        }
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public WebCourseVo selectWebCourseVoById(String id) {
        baseMapper.updateViewCountById(id);
        return baseMapper.selectWebCourseVoById(id);
    }

    @Override
    public CourseDto getCourseDtoById(String courseId) {
        return baseMapper.selectCourseDtoById(courseId);
    }

    @Override
    public void updateBuyCountById(String id) {
        baseMapper.updateBuyCountById(id);
    }
}
